//
//  DSBaseUintViewController.m
//  DSJY
//
//  Created by 冯华强 on 15/10/29.
//  Copyright (c) 2015年 HQ. All rights reserved.
//  抽取公共的CollectionViewController

#import "DSBaseUintViewController.h"
#import "DSCourseCell.h"
#import "DSUnitCell.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "MTConst.h"
#import "Masonry.h"

#define UnitCellW 160
#define UnitCellH 230
#define hMargin
@interface DSBaseUintViewController ()<UICollectionViewDelegate>
//流水布局和collectionView
@property (nonatomic, strong) UICollectionViewFlowLayout *collectionViewLayout;
@property (nonatomic, weak, readwrite) UICollectionView *collectionView;
/** 加载团购数据 */
@property (nonatomic, strong) NSMutableArray *deals;
/** 当前页数 */
@property (nonatomic, assign) NSUInteger currentPage;
/** 没有数据的图片 */
@property (nonatomic, weak) UIImageView *emptyDataImageView;
/** 加载数据的总数 */
@property (nonatomic, assign) NSInteger total_count;
@end

@implementation DSBaseUintViewController

#pragma mark - 懒加载
- (UICollectionViewFlowLayout *)collectionViewLayout
{
    if (_collectionViewLayout == nil) {
        _collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionViewLayout.itemSize = CGSizeMake(UnitCellW, UnitCellH);
    }
    return _collectionViewLayout;
}

- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.collectionViewLayout];
        collectionView.backgroundColor = MTGlobalBg;
        //允许垂直拉
        collectionView.alwaysBounceVertical = YES;
        //代理和数据源
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [self.view addSubview:collectionView];
        _collectionView = collectionView;
        
        [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    return _collectionView;
}

- (NSMutableArray *)deals
{
    if (_deals == nil) {
        _deals = [NSMutableArray array];
    }
    return _deals;
}

#pragma mark - 系统回调
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //绑定VM
    [self baseBindViewModel];
}

- (void)baseBindViewModel
{
    @weakify(self);
    // Register cell classes
    [self.collectionView registerNib:[UINib nibWithNibName:@"DSCourseCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:courseReuseIdentifier];
    [self.collectionView registerNib:[UINib nibWithNibName:@"DSUnitCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:unitReuseIdentifier];
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self checkNetWorkWithRefreshType:YQRefreshTypeHeader];
    }];
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        [self checkNetWorkWithRefreshType:YQRefreshTypeFooter];
    }];

    //默认先隐藏footer
    self.collectionView.mj_footer.hidden = NO;
}

#pragma mark - 屏幕旋转后就会调用IOS8新方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [self viewWillTransitionToSize:size];
}

- (void)viewWillTransitionToSize:(CGSize)size
{
    //    MTLog(@"%@", NSStringFromCGSize(size));
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    int cols = 0; //假设是3列
    cols = size.width == 1024 ? 5 : 4;
    CGFloat insets =  (size.width - cols * layout.itemSize.width) / (cols + 1);
    layout.sectionInset = UIEdgeInsetsMake(insets, insets, insets, insets);
    //设置每一行间距
    layout.minimumLineSpacing = insets;
}

@end
